package com.mf.service.impl.basic;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mf.entity.dto.basic.DtoUser;
import com.mf.entity.req.basic.ReqUserRole;
import com.mf.entity.req.basic.reqPublic.ReqPublicPaging;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import com.mf.entity.dto.basic.DtoUserRole;
import com.mf.entity.req.basic.search.SearchReqUserRole;
import com.mf.mapper.basic.MapperUserRole;
import com.mf.service.basic.ServiceUserRole;

import java.util.Set;
import java.util.stream.Collectors;


@Service
public class ImplServiceUserRole extends ServiceImpl<MapperUserRole, DtoUserRole> implements ServiceUserRole {

    @Resource
    private MapperUserRole mapperUserRole;



    /**
     * 参数查询条件
     */
    @Override
    public LambdaQueryWrapper<DtoUserRole> fillParams(SearchReqUserRole params) {
        return null;
    }

    /**
     * 根据角色id查询绑定数量
     */
    @Override
    public long countByRoleId(Integer roleId) {
        LambdaQueryWrapper<DtoUserRole> wrapper = loadQw();
        wrapper.eq(DtoUserRole::getRoleId, roleId);
        return count(wrapper);
    }

    /**
     * 根据角色id查询已关联用户
     */
    @Override
    public Page<DtoUser> pageUserByRole(Integer roleId, ReqPublicPaging reqPublicPaging) {
        return mapperUserRole.pageUserByRole(reqPublicPaging.getPage(), roleId);
    }

    /**
     * 根据角色id查询关联的用户id集合
     */
    @Override
    public Set<Integer> listUserByRole(Integer roleId) {
        final LambdaQueryWrapper<DtoUserRole> wrapper = loadQw();
        wrapper.eq(DtoUserRole::getRoleId, roleId);
        return list(wrapper).stream().map(DtoUserRole::getUserId).collect(Collectors.toSet());
    }
    /**
     * 根据角色id查询未关联用户
     */
    @Override
    public Page<DtoUser> pageUserByNoRole(Integer roleId, ReqPublicPaging reqPublicPaging) {
        return mapperUserRole.pageUserByNoRole(reqPublicPaging.getPage(), roleId);
    }



    /**
     * 根据用户ids关联角色
     */
    @Override
    public void addByAppend(ReqUserRole reqUserRole) {
        mapperUserRole.addByAppend(reqUserRole,StpUtil.getSession().getModel(SaSession.USER, DtoUser.class).getId());
    }
    /**
     * 根据角色id删除关联用户
     */
    @Override
    public void deleteByRoleId(ReqUserRole reqUserRole) {
        mapperUserRole.deleteByRoleId(reqUserRole);
    }
}